package src.com.atguigu03.list.exer2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * ClassName: PersonTest
 * Description:
 *定义方法public static int listTest(Collection list,String s)统计集合中指定元素出现的次数
 *
 * (1) 创建集合，集合存放随机生成的30个小写字母
 *
 * (2) 用listTest统计，a、b、c、x元素的出现次数
 *
 * (3) 效果如下
 * @Author liu
 * @Create 2024/11/24 17:23
 * @Version 1.0
 */
public class ListTest {
    public static void main(String[] args) {
        // 创建集合，集合存放随机生成的30个小写字母
        List list = new ArrayList();
        for (int i = 0; i < 30; i++) {
            //‘a’-->'z'[97,122]
            list.add((char)(Math.random()*(122-97+1)+97)+"");
        }
        System.out.println(list);

        int aCount = listTest(list,"a");
        int bCount = listTest(list,"b");
        int cCount = listTest(list,"c");
        int dCount = listTest(list,"d");
        int eCount = listTest(list,"e");

        System.out.println("a"+aCount);
        System.out.println("b"+bCount);
        System.out.println("c"+cCount);
        System.out.println("d"+dCount);
        System.out.println("e"+eCount);

    }


    // 用listTest统计，a、b、c、x元素的出现次数
    public static int listTest(Collection list, String s){
        int count = 0;
        for(Object obj:list){
            if(s.equals(obj)){
                count++;
            }
        }
        return count;
    }
}
